package cn.snmp.mutual.uk.stack;

import java.io.*;
import java.net.*;

import cn.snmp.mutual.uk.beans.UsmDiscoveryBean;


public class DefaultUsmAgent implements UsmAgent {
    static final String version_id = "@(#)$Id: DefaultUsmAgent.java,v 3.10 2009/03/05 15:51:42 birgita Exp $ Copyright Westhawk Ltd";
    public static final String LOCAL_HOST = "localhost";
    public static final int LOCAL_PORT = 161;
    private SnmpContextv3Basis context;
    private String hostname;
    private String hostaddress;
    private int port;

    public DefaultUsmAgent() {
        try {
            this.setAgentName("localhost");
        } catch (UnknownHostException exc) {
            this.hostname = "localhost";
            this.hostaddress = "127.0.0.1";
        }
        this.setAgentPort(161);
    }

    public String getSnmpEngineId() {
        final TimeWindow tWindow = TimeWindow.getCurrent();
        final String engineId = tWindow.getSnmpEngineId(this.hostaddress, this.port);
        return engineId;
    }

    public int getSnmpEngineBoots() {
        int boots = 1;
        TimeWindowNode node = null;
        final TimeWindow tWindow = TimeWindow.getCurrent();
        final String engineId = tWindow.getSnmpEngineId(this.hostaddress, this.port);
        if (engineId != null) {
            node = tWindow.getTimeLine(engineId);
        }
        if (node != null) {
            boots = node.getSnmpEngineBoots();
        }
        return boots;
    }

    public int getSnmpEngineTime() {
        int time = 1;
        TimeWindowNode node = null;
        final TimeWindow tWindow = TimeWindow.getCurrent();
        final String engineId = tWindow.getSnmpEngineId(this.hostaddress, this.port);
        if (engineId != null) {
            node = tWindow.getTimeLine(engineId);
        }
        if (node != null) {
            time = node.getSnmpEngineTime();
        }
        return time;
    }

    public void setSnmpContext(final SnmpContextv3Basis c) {
        this.context = c;
        try {
            this.discoverIfNeeded();
        } catch (PduException exc) {
            if (AsnObject.debug > 4) {
                System.out.println(this.getClass().getName() + ".setSnmpContext(): " + exc.getMessage());
            }
        } catch (IOException exc2) {
            if (AsnObject.debug > 4) {
                System.out.println(this.getClass().getName() + ".setSnmpContext(): " + exc2.getMessage());
            }
        }
    }

    public void setAgentName(final String host) throws UnknownHostException {
        this.hostname = host;
        final InetAddress ipAddr = InetAddress.getByName(this.hostname);
        this.hostaddress = ipAddr.getHostAddress();
    }

    public void setAgentPort(final int p) {
        this.port = p;
    }

    void discoverIfNeeded() throws IOException, PduException {
        UsmDiscoveryBean discBean = null;
        boolean isNeeded = false;
        final TimeWindow tWindow = TimeWindow.getCurrent();
        final String engineId = tWindow.getSnmpEngineId(this.hostaddress, this.port);
        if (engineId == null) {
            isNeeded = true;
            discBean = new UsmDiscoveryBean(this.hostname, this.port, this.context.getBindAddress(), this.context.getTypeSocket());
        }
        if (this.context.isUseAuthentication()) {
            if (isNeeded) {
                discBean.setAuthenticationDetails(this.context.getUserName(), this.context.getUserAuthenticationPassword(), this.context.getAuthenticationProtocol());
            } else if (!tWindow.isTimeLineKnown(engineId)) {
                isNeeded = true;
                discBean = new UsmDiscoveryBean(this.hostname, this.port, this.context.getBindAddress(), this.context.getTypeSocket());
                discBean.setAuthenticationDetails(this.context.getUserName(), this.context.getUserAuthenticationPassword(), this.context.getAuthenticationProtocol());
            }
            if (isNeeded && this.context.isUsePrivacy()) {
                discBean.setPrivacyDetails(this.context.getUserPrivacyPassword(), this.context.getPrivacyProtocol());
            }
        }
        if (isNeeded) {
            discBean.startDiscovery();
            discBean.freeResources();
        }
    }

    public long getUsmStatsUnknownEngineIDs() {
        return 0L;
    }

    public long getUsmStatsNotInTimeWindows() {
        return 0L;
    }
}
